package org.jianfa.rpc.consumer;

import org.jianfa.learn.common.service.IUserService;
import org.jianfa.rpc.consumer.client.RpcConsumer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class SimpleRpcConsumerApplication {

    public static void main(String[] args) throws Exception{
        //1.初始化客户端cliet
        RpcConsumer.init();

        //2.循环给服务器写数据
        int count = 1;
        while (true){
            Thread.sleep(10000);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String date = sdf.format(new Date());

            Map<String, IUserService> userServiceMap = RpcConsumer.getProxys();
            System.out.println(String.format("第%d轮请求一共有%d个服务器。时间: %s", count, userServiceMap.size(), date));

            for (Map.Entry<String, IUserService> userServiceEntry : userServiceMap.entrySet()) {
                String host = userServiceEntry.getKey();
                IUserService userService = userServiceEntry.getValue();
                System.out.println(String.format("    >>>> 客户端请求服务器 %s", host));

                String result = userService.sayHello("are you ok !!");
                System.out.println(String.format("    <<<< %s", result));
            }

            count++;
        }
    }
}
